package org.dragonnova.iot.business.cloud.transport.service;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

import java.io.IOException;
import java.net.InetSocketAddress;

public abstract class AbstractServer {
	private static int threadCounts = Runtime.getRuntime()
			.availableProcessors();
	protected InetSocketAddress bindAddr;
	protected final EventLoopGroup group;
	protected final EventLoopGroup workGroup;
	protected final ServerBootstrap serverBootstrap;
	protected final String osName;

	public AbstractServer(String host, int port) throws IOException {
		this.osName = System.getProperty("os.name");
		if (this.osName.equals("Linux")) {
			this.group = new EpollEventLoopGroup();
			this.workGroup = new EpollEventLoopGroup(threadCounts);
		} else {
			this.group = new NioEventLoopGroup();
			this.workGroup = new NioEventLoopGroup(threadCounts);
		}
		this.serverBootstrap = new ServerBootstrap();

		this.bindAddr = new InetSocketAddress(host, port);
	}

	protected InetSocketAddress getBindAddress() {
		return this.bindAddr;
	}

	protected abstract void start() throws Throwable;

	protected abstract void close() throws Throwable;
}
